package com.lgd.factoryattendance.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lgd.factoryattendance.Bean.ProjectDebugSpinner;
import com.lgd.factoryattendance.R;

import java.util.List;

/**
 * @Author lgd
 * @Date 2024/3/29 16:43
 * 下拉框 数据
 */
public class DebugLogHistoryAdapter extends BaseAdapter {

    private List<ProjectDebugSpinner> projectDebugSpinnerList;
    private Context mContext;
    private LayoutInflater layoutInflater;
    public DebugLogHistoryAdapter (List<ProjectDebugSpinner> projectDebugSpinnerList, Context context) {
        this.projectDebugSpinnerList = projectDebugSpinnerList;
        this.mContext = context;
        this.layoutInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return  projectDebugSpinnerList == null ? 0 :  projectDebugSpinnerList.size();
    }
    @Override
    public Object getItem(int position) {
        return projectDebugSpinnerList == null ? null : projectDebugSpinnerList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position; // 通常直接使用位置作为ID，除非你有特定的ID系统
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            // 如果没有可重用的视图，则创建新视图并初始化ViewHolder
            convertView = layoutInflater.inflate(R.layout.custom_spinner, parent, false);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.name);
//            holder.textView1 = (TextView) convertView.findViewById(R.id.Acode);
            convertView.setTag(holder); // 将ViewHolder附加到视图上，以便后续重用
        } else {
            // 如果有可重用的视图，则获取其ViewHolder
            holder = (ViewHolder) convertView.getTag();
        }
        // 使用ViewHolder来更新视图内容
        ProjectDebugSpinner projectDebugSpinner = projectDebugSpinnerList.get(position);
//        holder.textView1.setText(projectDebugSpinner.getProjectACode());
        holder.textView.setText(projectDebugSpinner.getProjectName()+"("+projectDebugSpinner.getProjectACode()+")");

        return convertView;
    }

    // ViewHolder类用于缓存视图的引用
    static class ViewHolder {
        TextView textView;
//        TextView textView1;
    }
}
